package com.mall.order.services;

import com.mall.order.OrderDeleteService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.dal.entitys.OrderItem;
import com.mall.order.dal.persistence.OrderItemMapper;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dal.persistence.OrderShippingMapper;
import com.mall.order.dto.DeleteOrderRequest;
import com.mall.order.dto.DeleteOrderResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

/**
 * @author：23117
 * @date: 2021/12/14
 */
@Service
@Component
@Slf4j
public class OrderDeleteServiceImpl implements OrderDeleteService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    OrderShippingMapper orderShippingMapper;

    @Override
    public DeleteOrderResponse deleteOrder(DeleteOrderRequest request) {
        DeleteOrderResponse deleteOrderResponse = new DeleteOrderResponse();

        try {
            request.requestCheck();
            Long orderId = Long.valueOf(request.getOrderId());
            int i = orderMapper.deleteByPrimaryKey(orderId);
            Example example = new Example(OrderItem.class);
            example.createCriteria().andEqualTo("orderId",orderId);
            int i1 = orderItemMapper.deleteByExample(example);
            int i2 = orderShippingMapper.deleteByPrimaryKey(orderId);

            if (i < 1 || i1 < 1 || i2 < 1){
                deleteOrderResponse.setCode(OrderRetCode.SYSTEM_ERROR.getCode());
                deleteOrderResponse.setMsg(OrderRetCode.SYSTEM_ERROR.getMessage());
                return deleteOrderResponse;
            }

            deleteOrderResponse.setCode(OrderRetCode.SUCCESS.getCode());
            deleteOrderResponse.setMsg(OrderRetCode.SUCCESS.getMessage());
        } catch (NumberFormatException e) {
            deleteOrderResponse.setCode(OrderRetCode.SYSTEM_ERROR.getCode());
            deleteOrderResponse.setMsg(OrderRetCode.SYSTEM_ERROR.getMessage());
        }

        return deleteOrderResponse;
    }
}
